﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace DAL
{
    public class MediaDAL
    {
#pragma warning disable CS0618 // “ConfigurationSettings.AppSettings”已过时:“This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings”
        string conn = ConfigurationSettings.AppSettings["con"].ToString();
#pragma warning restore CS0618 // “ConfigurationSettings.AppSettings”已过时:“This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings”
        /// <summary>
        /// 获取素材列表
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public DataSet getMedias(string type)
        {
            SqlParameter[] sp = new SqlParameter[]{
                new SqlParameter("@type",type)
            };
            string sqlstr = "select * from media where type = @type";
            //如果全选
            if (type == "")
            {
                sqlstr = sqlstr + " or 1 = 1";
            }
            DataSet ds = SqlHelper.ExecuteDataSet(conn,CommandType.Text,sqlstr,sp);
            return ds;
        }
        /// <summary>
        /// 批量插入素材
        /// </summary>
        /// <param name="type"></param>
        /// <param name="Media_id"></param>
        /// <param name="title"></param>
        /// <param name="update_time"></param>
        /// <returns>插入数量</returns>
        public int updates(DataTable dt, string tableName)
        {
            return SqlHelper.SqlBulkCopyInsert(conn,tableName,dt);
        }
        /// <summary>
        /// 清空表table
        /// </summary>
        /// <param name="table"></param>
        public void trunc(string table)
        {
            //SqlParameter[] sp = new SqlParameter[]{
            //    new SqlParameter("@table",table)
            //};
            //string sqlstr = "truncate table @table";
            //SqlHelper.ExecuteNonQuery(conn, CommandType.Text, sqlstr, sp); 
            SqlConnection sc = new SqlConnection(conn);
            SqlCommand scs = new SqlCommand("truncate table " + table,sc);
            sc.Open();
            scs.ExecuteNonQuery();
            sc.Close();
        }
        /// <summary>
        /// 删除素材
        /// </summary>
        /// <param name="media_id"></param>
        public int del(string media_id)
        {
            SqlParameter[] sp = new SqlParameter[]{
                new SqlParameter("@media_id",media_id)
            };
            string sqlstr = "delete from Media where media_id = @media_id";
            int i = SqlHelper.ExecuteNonQuery(conn,CommandType.Text,sqlstr,sp);
            return i;
        }
        /// <summary>
        /// 保存群发消息记录
        /// </summary>
        /// <param name="weixinh">公众号的微信号</param>
        /// <param name="mphelper">公众号群发助手的微信号</param>
        /// <param name="createtime">创建时间</param>
        /// <param name="msgType">消息类型</param>
        /// <param name="events">事件信息</param>
        /// <param name="msgID">群发的消息ID</param>
        /// <param name="status">群发的结果</param>
        /// <param name="totalCount">粉丝数</param>
        /// <param name="filterCount">过滤后准备发送的粉丝数</param>
        /// <param name="sentCount">发送成功的粉丝数</param>
        /// <param name="errorCount">发送失败的粉丝数</param>
        public int addMass(string weixinh,string mphelper,string createtime,string msgType,string events,int msgID,string status,int totalCount,int filterCount,int sentCount,int errorCount)
        {
            SqlParameter[] sp = new SqlParameter[]{
                new SqlParameter("@weixinh",weixinh),
                new SqlParameter("@mphelper",mphelper),
                new SqlParameter("@createtime",createtime),
                new SqlParameter("@msgType",msgType),
                new SqlParameter("@events",events),
                new SqlParameter("@msgID",msgID),
                new SqlParameter("@status",status),
                new SqlParameter("@totalCount",totalCount),
                new SqlParameter("@filterCount",filterCount),
                new SqlParameter("@sentCount",sentCount),
                new SqlParameter("@errorCount",errorCount)
            };
            string sqlstr = "insert into MassMsg values(@weixinh,@mphelper,@createtime,@msgType,@events,@msgID,@status,@totalCount,@filterCount,@sentCount,@errorCount)";
            int i = SqlHelper.ExecuteNonQuery(conn,CommandType.Text,sqlstr,sp);
            return i;
        }
        //查找群发记录
        public DataSet getMassMsg(int id)
        {
            SqlParameter[] sp = new SqlParameter[]{
                new SqlParameter("@id",id)
            };
            string sqlstr = "select * from MassMsg where id = @id or 1 = 1";
            DataSet ds = SqlHelper.ExecuteDataSet(conn,CommandType.Text,sqlstr,sp);
            return ds;
        }
    }
}
